package com.julian.observerPattern2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ActionEvent and ActionListener are also based on observer pattern.
 */
public class SwingObserverExample {
    JFrame frame;

    public static void main(String[] args) {
        SwingObserverExample example = new SwingObserverExample();
        example.go();
    }

    public void go(){
        frame = new JFrame();
        JButton button = new JButton("Should I do it?");
        button.addActionListener(new AngelListener());
        button.addActionListener(new DevilListener());
        frame.getContentPane().add(BorderLayout.CENTER, button);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setBounds(200,200,400,400);
    }

    class AngelListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Dont do it");
            System.out.println(e.getActionCommand());
            System.out.println(e.getModifiers());
            System.out.println(e.getWhen());
            System.out.println(e.paramString());
            System.out.println(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date()));
            System.out.println(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(e.getWhen()));
        }
    }

    class DevilListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Do it");
            System.out.println(e.getActionCommand());
            System.out.println(e.getModifiers());
            System.out.println(e.getWhen());
            System.out.println(e.paramString());
        }
    }
}
